import tool from "@/common/tool.js";
class HeaderMo {
  mixin = {
    data() {
      return {
        moreModel: false, //更多弹窗
        navModel: { //二级导航弹窗
          show: false,
          title: "",
          List: []
        },
        searchModel: { //搜索
          show: false,
          inputText: ""
        },
        bagModel: { //购物袋
          show: false,
          loading: false,
        },
        numberIndex: 0, //点击数量时的索引
        isChange: false,
        email: ""
      }
    },
    computed: {
      menuList() {
        return this.$store.state.menu.menuList;
      },
      userInfo() {
        return this.$store.state.user.userInfo;
      },
      cartList() { //购物车列表
        return this.$store.state.menu.cartList;
      },
      cartRefresh() { //购物车刷新
        return this.$store.state.menu.cartRefresh;
      }
    },
    methods: {
      //点击问题
      clickCell(fooIndex, acindex) {
        this.moreModel = false;
        this.footerList[fooIndex].show = false;
        let url = this.footerList[fooIndex].actions[acindex].url;
        this.$router.push({
          path: url
        })
      },
      //点击搜索
      clickSearch() {
        this.$store.commit("over/setSearchText", this.searchModel.inputText);
        this.searchModel.show = false;
        this.$router.push({
          path: "/search",
          query: {
            text: this.searchModel.inputText,
          }
        });

      },
      //点击logo
      clickLogo() {
        this.$router.push({
          path: "/",
        })
      },
      //点击一级导航
      clickNav(index, type) {
        if (type == "menu") {
          if (this.menuList[index].children !== undefined) {
            if (this.menuList[index].children.length != 0) {
              this.navModel.title = this.menuList[index].title;
              this.navModel.list = this.menuList[index].children;
              this.navModel.show = true;
            }
          } else {
            this.moreModel = false;
            this.$router.push({
              path: "/makeup/" + this.menuList[index].id + '/' + tool.urlCheckForMenuTitle(this.menuList[index].title),
            });
          }
        } else if (type == "foot") {
          this.navModel.title = this.footerList[index].title;
          this.navModel.list = this.footerList[index].actions;
          this.navModel.show = true;
        }
      },
      //点击二级导航
      clickNavSec(index) {
        if (this.navModel.list[[index]].url != undefined) {
          this.$router.push({
            path: this.navModel.list[[index]].url
          })
        } else {
          this.$router.push({
            path: "/makeup/" + this.navModel.list[[index]].id + '/' + tool.urlCheckForMenuTitle(this.navModel.list[[index]].title),
          })
        }
        this.navModel.show = false;
        this.moreModel = false;
      },
      //点击博客
      clickBlog() {
        this.moreModel = false;
        this.$router.push({
          path: "/blog"
        })
      },
      //点击用户
      clickUser() {
        this.moreModel = false;
        this.$router.push({
          path: "/user",
        })
      },
      //点击我的收藏
      clickColl() {
        if (this.isLogin == false) { //未登录
          this.$router.push({
            path: "/login",
            query: {
              redirect: "/" //重定向首页
            }
          });
        } else {
          this.$router.replace({
            path: "/user/collection"
          });
        }
      },
      //点击购物车
      clickCart() {
        this.bagModel.show = true;
        if (this.cartRefresh) {
          this.getCartList();
        }
      },
      //获取购物车列表,刷新购物车列表
      async getCartList() {
        if (this.isLogin) {
          this.bagModel.loading = true;
          this.$store.commit("menu/setCartList", []);
          let res = await this.$store.dispatch("menu/cartList");
          if (res.code == 0) {
            let cartList = res.data;
            for (let i = 0; i < cartList.length; i++) {
              cartList[i].number = parseInt(cartList[i].number);
              cartList[i].price = parseFloat(cartList[i].price);
              cartList[i]['attribute_me'] = cartList[i].attribute_text.toString();
            }
            this.$store.commit("menu/setCartRefresh", false); //取消刷新
            this.$store.commit("menu/setCartList", cartList);
          }
          this.bagModel.loading = false;
        } else {
          let cartList = tool.getStorage("cartList");
          for (let i = 0; i < cartList.length; i++) {
            cartList[i].number = parseInt(cartList[i].number);
            cartList[i].price = parseFloat(cartList[i].price);
            cartList[i]['attribute_me'] = cartList[i].attribute_text.toString();
          }
          this.$store.commit('menu/setCartList', cartList ? cartList : []); //保存购物车列表
        }
      },
      //点击number时的索引
      clickNumber(index) {
        this.numberIndex = index;
        this.isChange = true;
      },
      //改变数量
      async changeNumber(value) {
        // setTimeout(async () => {
        //   if (this.isChange) {
        //     this.tips.startLoadingToast();
        //     console.log(value);
        //     let res = await this.$store.dispatch("menu/changeProduct", {
        //       id: this.cartList[this.numberIndex].id,
        //       aid: this.cartList[this.numberIndex].aid,
        //       number: value
        //     });
        //     this.tips.closeToast();
        //     console.log(res);
        //     this.isChange = false;
        //     if (res.code == 0) {
        //       this.cartList[this.numberIndex].number = value;
        //       this.$store.commit("menu/setCartList", this.cartList);
        //     } else {
        //       this.$toast.fail(res.msg);
        //     }
        //   }
        // }, 100);
        this.tips.startLoadingToast();
        setTimeout(async () => {
          if (this.isChange) {
            if (this.Login) {
              let res = await this.$store.dispatch("menu/changeProduct", {
                id: this.cartList[this.numberIndex].id,
                aid: this.cartList[this.numberIndex].aid,
                number: value,
              });
              this.tips.closeToast();
              this.isChange = false;
              if (res.code == 0) {
                this.cartList[this.numberIndex].number = value;
                this.$store.commit("menu/setCartList", this.cartList);
                let totalPrice = 0;
                for (let i = 0; i < this.cartList.length; i++) {
                  totalPrice += this.cartList[i].price * this.cartList[i].number;
                }
                this.totalPrice = totalPrice;
              } else {
                this.$toast.fail(res.msg);
              }
            } else {
              let totalPrice = 0;
              for (let i = 0; i < this.cartList.length; i++) {
                totalPrice += this.cartList[i].price * this.cartList[i].number;
              }
              this.tips.closeToast();
              this.cartList[this.numberIndex].number = value;
              this.totalPrice = totalPrice;
              this.$store.commit("menu/setCartList", this.cartList); //保存购物车列表
              tool.setStorage("cartList", this.cartList);
            }
          }
        }, 300);
      },
      //删除购物车商品
      async deletePro(index) {
        this.tips.startLoadingToast();
        let res = await this.$store.dispatch("menu/changeProduct", {
          id: this.cartList[index].id,
          aid: this.cartList[index].aid,
          number: 0
        });
        this.tips.closeToast();
        this.isChange = false;
        if (res.code == 0) {
          this.cartList.splice(index, 1);
        } else {
          this.$toast.fail(res.msg);
        }
      },
      //订单结算
      async clickCheckOut() {
        if (this.cartList.length == 0) {
          this.$toast("No items in shopping cart");
          return;
        }
        this.tips.startLoadingToast();
        let proList = [];
        this.$store.commit("order/setRefreshPro", true);
        for (let i = 0; i < this.cartList.length; i++) {
          if (this.cartList[i].status) {
            proList.push(this.cartList[i]);
          }
        }
        tool.setStorageAsyn("cartCheckList", proList, () => {
          setTimeout(() => {
            this.$router.push({
              path: "/user/order/checkorder"
            });
            this.bagModel.show = false;
            this.tips.closeToast();
            this.$store.commit("menu/setCartList", []);
            tool.setStorage("cartList", []);
          }, 1000);
        });
      },
      //点击订阅
      async clickSubscribe() {
        let reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        let bool = reg.test(this.email);
        if (this.email == "") {
          this.$toast("Please input email")
        } else if (!bool) {
          this.$toast("Please enter the correct email address")
        } else {
          let res = await this.$store.dispatch("over/subscribe", {
            email: this.email
          });
          if (res.code == 0) {
            this.$toast("Subscription succeeded")
          } else {
            this.$toast.fail(res.msg);
          }
        }
      }
    }
  }
}
export default new HeaderMo();
